package cn.m1c.cntt.wuhan2020.cnttlifegoods.person.service

import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.enums.StatusCode
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.model.ResultVo
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.utils.AssertUtil
import cn.m1c.cntt.wuhan2020.cnttlifegoods.person.dao.UserRepository
import cn.m1c.cntt.wuhan2020.cnttlifegoods.person.vo.UserVo
import cn.m1c.cntt.wuhan2020.cnttlifegoods.person.vo.UserWxVo
import com.alibaba.fastjson.JSON
import org.apache.logging.log4j.kotlin.Logging
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional
import org.springframework.util.DigestUtils
import java.util.concurrent.TimeUnit
import javax.annotation.Resource


@Service
class UserService : Logging{
    @Autowired
    lateinit var userRepository: UserRepository

    @Resource
    lateinit var redisTemplate : RedisTemplate<String, UserVo>

    @Transactional(propagation = Propagation.REQUIRED)
    fun login(userVo: UserVo): ResultVo<HashMap<String, String>> {
        var resultVo = ResultVo<HashMap<String,String>>(data = HashMap())
        var userList = userRepository.queryByUserName(userVo.userName!!)
        if(userList.isEmpty()){
            resultVo.code = Integer.MIN_VALUE;
            resultVo.message = "该用户不存在"
        }else{
            var dabUservo = userList.get(0);
            if(dabUservo.password.equals(userVo.password)){
                var token = DigestUtils.md5DigestAsHex(dabUservo.id!!.toByteArray())
                redisTemplate.opsForValue().set(token,dabUservo,30L, TimeUnit.DAYS)
                resultVo.data!!.put("token",token);
                println(JSON.toJSONString(redisTemplate.opsForValue().get(token)));
            }else{
                resultVo.code = Integer.MIN_VALUE;
                resultVo.message = "密码不对"
            }
        }
        return resultVo
    }
    @Transactional(propagation = Propagation.REQUIRED)
    fun loginWx(userWxVo: UserWxVo):ResultVo<HashMap<String,String>>{
        logger.debug("userWxvo is ${JSON.toJSONString(userWxVo)}")
        var resultVo = ResultVo<HashMap<String,String>>(data = HashMap())
        var userVoList = userRepository.queryByOpenId(userWxVo.openId)
        var userVo = UserVo()
        if (userVoList.isNotEmpty()){
            userVo = userVoList.get(0)
        }else{
            userVo.openId = userWxVo.openId
        }
        userVo.nickName = userWxVo.nickName
        userVo.gender = userWxVo.gender
        userVo.avatarUrl = userWxVo.avatarUrl

        userRepository.save(userVo)

        var token = DigestUtils.md5DigestAsHex(userVo.id!!.toByteArray())
        redisTemplate.opsForValue().set(token,userVo,30L, TimeUnit.DAYS)
        resultVo.data!!.put("token",token);

        return resultVo
    }
    @Transactional(propagation = Propagation.REQUIRED)
    fun register(userVo: UserVo):ResultVo<HashMap<String,String>>{
        var resultVo = ResultVo<HashMap<String,String>>(data = HashMap())
        var userList = userRepository.queryByUserName(userVo.userName!!)
        var userList2 = userRepository.queryByPhone(userVo.phone!!)
        if(userList.isEmpty() && userList2.isEmpty()){
            userRepository.insert(userVo)
            var token = DigestUtils.md5DigestAsHex(userVo.id!!.toByteArray())
            redisTemplate.opsForValue().set(token,userVo,30L, TimeUnit.DAYS)
            resultVo.data!!.put("token",token);
            println(JSON.toJSONString(redisTemplate.opsForValue().get(token)));
        }else{
            resultVo.code = Integer.MIN_VALUE;
            resultVo.message = "您录入的手机号或者用户名重复"
        }
        return resultVo
    }

}